//
//  SchoolSelectViewController.m
//  Comma
//
//  Created by 刘轩博 on 15/1/17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CollegeRegisterViewController.h"
#import "AFNetworking.h"

@interface CollegeRegisterViewController ()

@end

@implementation CollegeRegisterViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // 注册消息, 当TextField的文字改变, 执行对应的方法
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchCollegeWithName) name:UITextFieldTextDidChangeNotification object:self.collegeView.collegeField];
        // 注册消息, 当选中大学的时候, 模态消失
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collegeSelected:) name:@"COLLEGESELECTED" object:nil];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collegeView = [[CollegeRegisterView alloc] initWithExitImage:@"关闭按钮.png" collegeText:@"请输入大学名称"];
    
    // 设置collegeView为主视图
    self.view = self.collegeView;
    
    // 数组初始化
    self.collegeView.tViewArray = [NSMutableArray array];
    self.allCollegeArray = [NSMutableArray array];
    
    // 为退出按钮添加点击事件
    [self.collegeView.exitButton addTarget:self action:@selector(exitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 开始网络请求数据
    [self connectWithUrl];
    
    
}

#pragma mark - 查找所有的大学数据
- (void)connectWithUrl
{
    // 网络请求JSON数据
    NSURL *url = [NSURL URLWithString:@"http://www.isu.so/schools.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
        self.allCollegeArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [_collegeView.tViewArray removeAllObjects];
        [_collegeView.tViewArray addObjectsFromArray:_allCollegeArray];
        
        
        [self.collegeView createTableView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    // 加入队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
}

#pragma mark - 随着输入文字的不同, 进行搜索大学
- (void)searchCollegeWithName
{
    UITextField *field = self.collegeView.collegeField;
    [_collegeView.tViewArray removeAllObjects];
    if ([field.text isEqualToString:@""]) {
        [_collegeView.tViewArray addObjectsFromArray:_allCollegeArray];
    }
    for (NSDictionary *dic in _allCollegeArray) {
        NSString *name = [dic objectForKey:@"name"];
        if ([name hasPrefix:field.text]) {
            [_collegeView.tViewArray addObject:dic];
        }
    }
    [_collegeView.tableView reloadData];
}

#pragma mark -
- (void)collegeSelected:(NSNotification *)notify
{
    NSString *name = [notify.userInfo objectForKey:@"name"];
    [self.delegate assignCollegeName:name];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 返回点击事件
- (void)exitButtonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
